#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from abc import ABC, abstractmethod
import argparse
import sys as _sys

class ArgumentError(Exception):
    pass

class SubcmdArgp(argparse.ArgumentParser):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_help = False
        self.exit_on_error = False

        # TODO: add help action
        # self.register('help', action='help')

    def error(self, message):
        """error(message: string)

        Prints a usage message incorporating the message to stderr and
        exits.

        If you override this in a subclass, it should not return -- it
        should either exit or raise an exception.
        """
        self.print_usage(_sys.stderr)
        args = {'prog': self.prog, 'message': message}
        raise ArgumentError(('%(prog)s: error: %(message)s') % args)


class CToolsCmd(ABC, object):
    @classmethod
    def can_work(cls):
        return True

    @abstractmethod
    def name(cls):
        return ''

    @abstractmethod
    def description(cls):
        return ''

    @classmethod
    def is_match(cls, name) -> bool:
        return cls.name() and name == cls.name()

    @abstractmethod
    def do_command(self, argv:list[str]):
        pass

    def __repr__(self):
        # return "<%s: %s>" % (self.__class__.__name__, self.name)
        return f"<{self.__class__.__name__}: {self.name}>"
